package client;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileDataProcessor implements DataReader{
	File file;
	
	public FileDataProcessor(){
		file = null;
	}
	public FileDataProcessor(Properties props){
		file = new File(props.getProperty("filepath"));
	}
	
	public Record findRecord(String strToFind) {
		BufferedReader fileReader = null;
		Record result = null;
		try {
			fileReader = new BufferedReader(new FileReader(file));
		} catch (FileNotFoundException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String line = null;
		try {
			line = fileReader.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = 1;
		int id = 0;
		while(line != null){
			if(i%2 == 1 && line.length()>2 && line.equals(strToFind)){
				try {
					result = new Record(id, line, fileReader.readLine());
					id++;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			try {
				line = fileReader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}
}